<!DOCTYPE html>
<!--
Copyright (c) 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/extras/importer/linux_perf/parser.html">

<script>
'use strict';

/**
 * @fileoverview Parses regulator events in the Linux event trace format.
 */
tr.exportTo('tr.e.importer.linux_perf', function() {
  const ColorScheme = tr.b.ColorScheme;
  const Parser = tr.e.importer.linux_perf.Parser;

  /**
   * Parses linux regulator trace events.
   * @constructor
   */
  function RegulatorParser(importer) {
    Parser.call(this, importer);

    importer.registerEventHandler('regulator_enable',
        RegulatorParser.prototype.regulatorEnableEvent.bind(this));
    importer.registerEventHandler('regulator_enable_delay',
        RegulatorParser.prototype.regulatorEnableDelayEvent.bind(this));
    importer.registerEventHandler('regulator_enable_complete',
        RegulatorParser.prototype.regulatorEnableCompleteEvent.bind(this));
    importer.registerEventHandler('regulator_disable',
        RegulatorParser.prototype.regulatorDisableEvent.bind(this));
    importer.registerEventHandler('regulator_disable_complete',
        RegulatorParser.prototype.regulatorDisableCompleteEvent.bind(this));
    importer.registerEventHandler('regulator_set_voltage',
        RegulatorParser.prototype.regulatorSetVoltageEvent.bind(this));
    importer.registerEventHandler('regulator_set_voltage_complete',
        RegulatorParser.prototype.regulatorSetVoltageCompleteEvent.bind(this));

    this.model_ = importer.model_;
  }

  // Matches the regulator_enable record
  const regulatorEnableRE = /name=(.+)/;

  // Matches the regulator_disable record
  const regulatorDisableRE = /name=(.+)/;

  // Matches the regulator_set_voltage_complete record
  const regulatorSetVoltageCompleteRE = /name=(\S+), val=(\d+)/;

  RegulatorParser.prototype = {
    __proto__: Parser.prototype,

    /**
     * Get or create a counter with one series.
     */
    getCtr_(ctrName, valueName) {
      const ctr = this.model_.kernel
          .getOrCreateCounter(null, 'vreg ' + ctrName + ' ' + valueName);
      // Initialize the counter's series fields if needed.
      if (ctr.series[0] === undefined) {
        ctr.addSeries(new tr.model.CounterSeries(valueName,
            ColorScheme.getColorIdForGeneralPurposeString(
                ctrName + '.' + valueName)));
      }
      return ctr;
    },

    /**
     * Parses regulator events and sets up state in the importer.
     */
    regulatorEnableEvent(eventName, cpuNum, pid, ts, eventBase) {
      const event = regulatorEnableRE.exec(eventBase.details);
      if (!event) return false;

      const name = event[1];

      const ctr = this.getCtr_(name, 'enabled');
      ctr.series[0].addCounterSample(ts, 1);

      return true;
    },

    regulatorEnableDelayEvent(eventName, cpuNum, pid, ts, eventBase) {
      return true;
    },

    regulatorEnableCompleteEvent(eventName, cpuNum, pid, ts,
        eventBase) {
      return true;
    },

    regulatorDisableEvent(eventName, cpuNum, pid, ts, eventBase) {
      const event = regulatorDisableRE.exec(eventBase.details);
      if (!event) return false;

      const name = event[1];

      const ctr = this.getCtr_(name, 'enabled');
      ctr.series[0].addCounterSample(ts, 0);

      return true;
    },

    regulatorDisableCompleteEvent(eventName, cpuNum, pid, ts,
        eventBase) {
      return true;
    },

    regulatorSetVoltageEvent(eventName, cpuNum, pid, ts, eventBase) {
      return true;
    },

    regulatorSetVoltageCompleteEvent(eventName, cpuNum, pid, ts,
        eventBase) {
      const event = regulatorSetVoltageCompleteRE.exec(eventBase.details);
      if (!event) return false;

      const name = event[1];
      const voltage = parseInt(event[2]);

      const ctr = this.getCtr_(name, 'voltage');
      ctr.series[0].addCounterSample(ts, voltage);

      return true;
    }

  };

  Parser.register(RegulatorParser);

  return {
    RegulatorParser,
  };
});
</script>
